package com.jy.modules.cims.transexception.service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.component.common.service.TCMmTransExceptionService;
import com.jy.modules.cims.data.common.dto.TCMmTransConfigDTO;
import com.jy.modules.cims.data.common.dto.TCMmTransExceptionDTO;
import com.jy.modules.cims.data.dto.credit.RedeemResultDto;

@Service("com.jy.modules.cims.transexception.service.TransExceptionService")
public class TransExceptionService {
	private static Logger logger = LoggerFactory.getLogger(TransExceptionService.class);
	
	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmTransExceptionService")
	private TCMmTransExceptionService transExceptionService;
	
	
	/**
	 * @throws Exception 
	 * 插入异常事务日志,如果有该条交易流水的日志，则更新日志，如果没有该条交流流水的日志，则插入日志
     * @throws Exception 
     * @Title insertTransException
     * @Description TODO
     * @return 
     * @author yanjingying
     * @date 2015-12-17-下午11:47:00
     * @update
     * @throws
	 * */
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public Long saveTransException(TCMmTransConfigDTO transConfigDTO, RedeemResultDto redeemResultDto, String retCode, String dealCode) throws Exception{
		Long id = null;   //技术主键
		TCMmTransExceptionDTO  dto = new TCMmTransExceptionDTO();
		dto.setDealCode(dealCode);    //交易流水号
		dto.setTransEnName(transConfigDTO.getTransEnName());   //事务英文名称
		dto.setTransNo(transConfigDTO.getTransNo());   //事务编号
		Map<String, Object> searchParams = new HashMap<String, Object>();
		searchParams.put("dto", dto);
		List<TCMmTransExceptionDTO>  transExceptionDTOList = transExceptionService.searchTCMmTransException(searchParams);
		if(transExceptionDTOList != null && transExceptionDTOList.size() > 0){
			//update信息
			dto = transExceptionDTOList.get(0);
			if(redeemResultDto.getCoreTransNo() != null && !("".equals(redeemResultDto.getCoreTransNo()))){
				dto.setCoreTransNo(redeemResultDto.getCoreTransNo());   //核心交易流水号，指接口调用的流水号
				dto.setFrontTransTime(Timestamp.valueOf(redeemResultDto.getRetTime()));  //合作机构交易时间
				dto.setRetTime(Timestamp.valueOf(redeemResultDto.getRetTime()));          //核心交易时间
			}
			dto.setTransDesc(CimsConstant.RET_CODE_NO_INVOKE.equals(retCode) ? "无需调用该事务": "") ;
			dto.setRetMsg(redeemResultDto.getRetMsg());
			dto.setFrontTransNo(redeemResultDto.getFrontTransNo()); //合作机构机构交易流水号，指接口调用的流水号
			dto.setRetCode(retCode); //0000：成功  0001：失败  9999:日终处理中
			dto.setBusiCode(transConfigDTO.getBusiCode());       //业务编码
			dto.setInterfaceNo(transConfigDTO.getInterfaceNo()); //接口编码
			dto.setExecTimes(dto.getExecTimes() + 1);   //执行次数
			transExceptionService.updateTCMmTransException(dto);
			id = dto.getId();   //返回主键
		}else{
			//insert信息
			if(redeemResultDto.getCoreTransNo() != null && !("".equals(redeemResultDto.getCoreTransNo()))){
				dto.setCoreTransNo(redeemResultDto.getCoreTransNo());   //核心交易流水号，指接口调用的流水号
				dto.setFrontTransTime(Timestamp.valueOf(redeemResultDto.getRetTime()));  //合作机构交易时间
				dto.setRetTime(Timestamp.valueOf(redeemResultDto.getRetTime()));          //核心交易时间
			}
			dto.setTransDesc(CimsConstant.RET_CODE_NO_INVOKE.equals(retCode) ? "无需调用该事务": "") ;
			dto.setRetMsg(redeemResultDto.getRetMsg());
			dto.setFrontTransNo(redeemResultDto.getFrontTransNo()); //合作机构机构交易流水号，指接口调用的流水号
			dto.setRetCode(retCode); //0000：成功  0001：失败  9999:日终处理中
			dto.setBusiCode(transConfigDTO.getBusiCode());       //业务编码
			dto.setInterfaceNo(transConfigDTO.getInterfaceNo()); //接口编码
			dto.setExecTimes(new Long(1));   //执行次数
			id = transExceptionService.insertTCMmTransException(dto);    //返回主键
		}
		return id;
	}

}
